package com.lgq.cfw.controller.permission;

import com.lgq.cfw.bean.TablePermission;
import com.lgq.cfw.bean.message.Message;
import com.lgq.cfw.service.PermissionService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lin
 * @date 2019-02-10 15:39
 **/
@RestController
@RequestMapping(value="/permission/permission")
public class PermissionController {
  @Autowired
  PermissionService permissionService;

  /**
   * 获取所有权限
   * @return 所有权限的集合
   */
  @RequestMapping(value = "/getAllPermission",method = RequestMethod.GET)
  public List<TablePermission> getAllPermission(){
    return permissionService.getAllPermission();
  }

  /**
   * 通过角色id获取权限
   * @param roleId 角色id
   * @return 该角色所有的权限
   */
  @RequestMapping(value = "/{roleId}")
  public List<TablePermission> getPermissionById(@PathVariable(value = "roleId") Integer roleId){
    return permissionService.getPermissionById(roleId);
  }

  /**
   * 删除权限
   * @param permissionId 权限id
   * @return 处理信息
   */
  @RequestMapping(value = "/{permissionId}",method = RequestMethod.DELETE)
  public Message deletePermission(@PathVariable(value = "permissionId") Integer permissionId){
    Boolean flag=permissionService.deletePermission(permissionId);
    if (flag){
      return Message.success("处理成功",null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 增加权限
   * @param pid 权限的父菜单
   * @param name 要增加的权限名称
   * @return 处理信息
   */
  @RequestMapping(value = "/add",method = RequestMethod.POST)
  public Message addPermission(@RequestParam(value = "pid")Integer pid,@RequestParam(value = "name") String name){
    permissionService.addPermission(pid,name);
    return Message.success("处理成功",null);
  }

  /**
   * 更新权限信息
   * @param tablePermission 前台传入的权限信息
   * @return 处理信息
   */
  @RequestMapping(value = "/update",method = RequestMethod.POST)
  public Message updatePermission(TablePermission tablePermission){
    permissionService.updatePermission(tablePermission);
    return Message.success("处理成功",null);
  }
}
